<?php
class News1_AdminCategoryController extends  Zendvn_Controller_Action{
	
	public function init(){
		parent::init();		
		$this->_templateObj->setPath(TEMPLATE_PATH . '/admin/white');		
		$this->_templateObj->load();	
		
		/*====================================================
		 * Load Zendvn Translate for Controller 
		 *====================================================*/
		$this->_langObj->setLangFile(array('contact.category.tmx','validate.tmx'));		
		Zend_Registry::set('Zend_Translate',$this->_langObj->generate());
		
		
		$ss = new Zend_Session_Namespace($this->_ssNamespace);
		/*xoa session
			$ss->unsetAll();
		*/
		if(empty($ss->col)){
			$ss->col = 'id';
			$ss->order = 'DESC';
		}
		$ss->setExpirationSeconds(600);
		$this->_arrParam['arrFilter'] = $ss->getIterator();
		$this->view->arrParam = $this->_arrParam;
		
		$config = new Zendvn_Controller_Config(array('module'=>$this->_arrParam['module'],
												'fileConfig'=>'news1Category.ini'));
		$this->_config = $config->getconfig();		
	}
	

	/*===============================================
	 * ACTION INDEX
	 *===============================================*/
	public function indexAction(){
		$this->view->Title = $this->view->translate('News1  Manager') . ' : '
							 .  $this->view->translate('Items') . ' : '
							 .  $this->view->translate('List');
		$this->view->headTitle($this->view->Title,true);
		
		$tableCat = new Model_Table_News1Category();
		$this->view->Item = $tableCat->listItem($this->_arrParam);
		$this->view->slbSearchFiels = $tableCat->itemInSelect($this->_arrParam);
	}
	
	
	/*===============================================
	 * ACTION STATUS
	 *===============================================*/	
	public function statusAction(){
		
		$tableCat = new Model_Table_News1Category();
		
		if($this->_arrParam['type'] == 'multi'){
			$tableCat->statusItem($this->_arrParam,array('task'=>'multi'));
		}else{
			$tableCat->statusItem($this->_arrParam);
		}
		
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}	
	

	
	/*===============================================
	 * Filter left
	 *===============================================*/
	public function filterAction(){
		
		$ss = new Zend_Session_Namespace($this->_ssNamespace);
		
		if($this->_arrParam['type'] == 'search'){
			$ss->keyword = $this->_arrParam['keyword'];
			$ss->searchField = $this->_arrParam['search-field'];
		}
		
		if($this->_arrParam['type'] == 'clear'){
			$ss->keyword = '';
			$ss->searchField = '';
		}
		
		if($this->_arrParam['type'] == 'filter'){
			$ss->status = $this->_arrParam['status'];
			$ss->parents = $this->_arrParam['catId'];
		}
		
		if($this->_arrParam['type'] == 'sort'){
			$ss->col = $this->_arrParam['col'];
			$ss->order = $this->_arrParam['order'];
		}
		
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}
	
	/*===============================================
	 * ACTION INFO
	 *===============================================*/
	public function infoAction(){
		$this->view->Title = $this->view->translate('News1  Manager') . ' : '
							 .  $this->view->translate('Category') . ' : '
							 .  $this->view->translate('Info');
		$this->view->headTitle($this->view->Title,true);
		
		$tableCat 			= new Model_Table_News1Category();
		$this->view->Item   = $tableCat->getItem($this->_arrParam,array('task'=>'info'));
	

	 	$this->view->config = $this->_config['category'];

	}	
	
	/*===============================================
	 * ACTION EDIT
	 *===============================================*/
	public function editAction(){
		$this->view->Title = $this->view->translate('News1  Manager') . ' : '
							 .  $this->view->translate('Category') . ' : '
							 .  $this->view->translate('Edit');
		$this->view->headTitle($this->view->Title,true);
		
		$tableCat = new Model_Table_News1Category();
		
		if($this->_request->isPost()){	
			//kiểm tra dữ liệu		
			$validator = new News1_Form_ValidateNews1Category($this->_arrParam);			
			if($validator->isVaild()){
				$this->view->errors 	= $validator->getMessageErrors();
				$this->view->Item 		= $validator->getData();				
			}else{
				//xuất thông báo cho người dùng 
				$this->_helper->flashMessenger->addMessage(array('success'=>'The update was successful'));
				$data =  $validator->getData();	
									
				$tableCat->saveItem($data,array('task'=>'admin-edit'));
				if($this->_arrParam['type'] == 'back'){
					$this->_redirect($this->_mainAction);
				}else{
					$this->_redirect($this->_currentController 
									 . '/' . $this->_arrParam['action'] 
									 . '/id/' . $this->_arrParam['id']);
				}
			}			
		}
		$this->view->Item 				 	= $tableCat->getItem($this->_arrParam);
		$this->_arrParam['language_code'] 	= $this->view->Item['language_code'];
		$this->view->slbParents 			= $tableCat->itemInSelect($this->_arrParam);
		$this->_helper->viewRenderer('form');
	}

	
	/*===============================================
	 * ACTION DELETE
	 *===============================================*/	
	public function deleteAction(){
				
		$tableCat = new Model_Table_News1Category();
		
		if($this->_arrParam['type'] == 'multi'){
			$tableCat->deleteItem($this->_arrParam, array('task'=>'multi'));
		}else{
			$tableCat->deleteItem($this->_arrParam);
		}
		
		//xuất thông báo cho người dùng
		$this->_helper->flashMessenger->addMessage(array('success'=>'The delete was successful'));
		$this->_redirect($this->_mainAction);
		$this->_helper->viewRenderer->setNoRender();
	}

	/*===============================================
	 * ACTION ADD
	 *===============================================*/		
	public function addAction(){
		$this->view->Title = $this->view->translate('News1  Manager') . ' : '
							 .  $this->view->translate('Category') . ' : '
							 .  $this->view->translate('Add');
		$this->view->headTitle($this->view->Title,true);
		
		$tableCat = new Model_Table_News1Category();
		$this->view->slbParents = $tableCat->itemInSelect($this->_arrParam);
		if($this->_request->isPost()){	
			//kiểm tra dữ liệu		
			$validator = new News1_Form_ValidateNews1Category($this->_arrParam);			
			if($validator->isVaild()){
				$this->view->errors = $validator->getMessageErrors();
				$this->view->Item 	= $validator->getData();				
			}else{
				//xuất thông báo cho người dùng
				$this->_helper->flashMessenger->addMessage(array('success'=>'The update was successful'));
				$data =  $validator->getData();	
				$tableCat->saveItem($data,array('task'=>'admin-add'));
				if($this->_arrParam['type'] == 'back'){
					$this->_redirect($this->_mainAction);
				}else{
					$this->_redirect($this->_currentController . '/' . $this->_arrParam['action']);
				}
			}
		}
		
		$this->_helper->viewRenderer('form');
	}

	/*===============================================
	 * ACTION SORT
	 *===============================================*/		
	public function sortAction(){		
		$tableCat = new Model_Table_News1Category();
		$tableCat->sortItem($this->_arrParam,array('column'=>'ordering'));
		$this->_redirect($this->_mainAction);
		$this ->_helper->viewRenderer->setNoRender();

	}	
	
	
	/*===============================================
	 * ACTION REMOVE
	 *===============================================*/		
	public function removePicAction(){
		//1.Xoa hinh
		//2.Cap nhat database
		$config = $this->_config['category'];
		$upload = new Zendvn_File_Upload();
		$tableContact = new Model_Table_News1Category();		

		$upload->remove($config['uploadDir'],$this->_arrParam['file']);
		$upload->remove($config['thumbDir'],$this->_arrParam['file']);			
		$tableContact->update(array('picture'=>''), 'id = ' . $this->_arrParam['id']);

		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout()->disableLayout();
	}
	
}